package com.example.movie.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.movie.domain.Comment;
import com.example.movie.domain.Movie;
import com.example.movie.mapper.CommentMapper;
import com.example.movie.mapper.MovieMapper;
import com.example.movie.service.CommentService;
import com.example.movie.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CountScoreJob {

    @Autowired
    MovieService movieService;
    @Autowired
    CommentService commentService;


    @Scheduled(cron = "0/20 * * * * ? ")
    public void countScore(){
        List<Movie> movies = movieService.getRecentMoviesIds();
        for (Movie movie : movies) {
            List<Comment> commentsByMId = commentService.getCommentsByMId(movie.getMId());
            double score = commentsByMId.stream().mapToInt(Comment::getScore).average().orElse(0.0);
            movie.setMScore(score);
            movieService.updateById(movie);
        }
    }
}
